<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      :disabled="message.length===0"
      @click="submit"
    >发布</van-button>
  </div>
</template>

<script>
import {addComment} from '@/api/comment'
export default {
  name: 'CommentPost',
  data () {
    return {
      message: ''
    }
  },
  // 祖先与后代通信 后代注入数据
  inject:['articleId'],
  props:{
    // 文章id 或者评论id 
      target:{
          type:[String,Number],
          required:true
      },
      // 判断操作类型
      type:{
        default:'a',
        validator(val) {
          return ['a','c'].includes(val)
        }
      }
  },
  methods: {
    async  submit() {
        try {
             let {data} =await addComment({
             target:this.target, //文章id或者评论id
             content:this.message, //评论内容
             art_id: this.type==='c' ? this.articleId : null //文章id
         })
         console.log(data);
         this.$toast.success('发布成功')
         this.message=''
         this.$emit('close')
         this.$emit('update-list',data.data)
        } catch (error) {
            console.log(error);
        }
      }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>